This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
This code reads in the data we will need from the csv files:
# Note, changed paths to use here::here() since .Rmd is in different folder from Rproj folder
gapminder2011 <- read_csv(here::here("data","Gapminder_vars_2011.csv"))
gapminder2011_long <- read_csv(here::here("data","Gapminder_vars_2011_long.csv"))
pasilla_data <- read_csv(here::here("data","gene_expr_pasilla_results.csv"))
This is the code from the slides. Change the following code to do the following:
members_oecd_g77 and fill to WaterSourcePrct_2011_quartposition="dodge" as an argument to geom_bar()scale_x_discrete(limits = c(NA,"others","oecd","g77"))ggplot(data = gapminder2011,
aes(x = four_regions,
fill = eight_regions)) +
geom_bar() +
theme(
axis.text.x = element_text(angle = -30, hjust = 0)) +
labs(x = "World Regions",
y = "Number of countries",
title = "Barplot") +
theme_bw() +
scale_fill_viridis_d(name = "Subregions")
ggplot(data = gapminder2011,
aes(x = members_oecd_g77,
fill = WaterSourcePrct_2011_quart)) +
geom_bar(position="dodge") +
# if you want to keep width the same:
#geom_bar(position = position_dodge2(preserve="single"))+
theme(
axis.text.x = element_text(angle = -30, hjust = 0)) +
labs(x = "OECD g77 Members",
y = "Number of countries",
title = "Barplot") +
theme_bw() +
scale_x_discrete(limits = c(NA,"others","oecd","g77")) +
scale_fill_viridis_d(name = "World Bank Region", option = "C") +
coord_flip()
This is the code from the slides. Change the following code to do the following:
?geom_histogram in the console to find the argument)geom_density() with alpha = 0.4aes(y=..density..) to the histogram function arguments.four_regionstheme_bw()scale_fill_viridis_d)eight_regions, color by four_regionsggplot(data = gapminder2011,
aes(x = LifeExpectancyYrs,
fill = four_regions)) +
geom_histogram() +
scale_fill_discrete(
name = "Regions",
labels = c("Africa", "Americas",
"Asia", "Europe")
) +
labs(
x = "Life Expectancy (years)",
title = "Histogram"
) +
ggthemes::theme_economist() +
theme(
legend.position="bottom"
)
ggplot(data = gapminder2011,
aes(x = LifeExpectancyYrs,
color = four_regions,
fill = eight_regions)) +
geom_histogram(aes(y=..density..),
binwidth = 1.5) +
geom_density(alpha = 0.4) +
scale_fill_viridis_d(
name = "Regions"
) +
facet_wrap(~four_regions) +
labs(
x = "Life Expectancy (years)",
title = "Histogram and Density of Life Expectancy"
) +
theme_bw() +
# remove legend for color
scale_color_discrete(guide = FALSE)
This is the code from the bubble plot.
IncomePPmembers_oecd_g77p and call ggplotly(p) but be sure to add the name of the country to the hover box (hint: use key)ggplot(data = gapminder2011,
aes(x = FoodSupplykcPPD,
y = LifeExpectancyYrs,
color = four_regions,
size = population)) +
geom_point(alpha = 0.4) +
scale_color_colorblind(
name = "Regions",
labels = c("Africa", "Americas",
"Asia", "Europe")
) +
scale_size(
name = "Population Size (millions)",
breaks = c(1e08,5e08,1e09),
labels = c(100,500,1000)
) +
hrbrthemes::theme_ipsum() +
labs(
x = "Daily Food Supply PP (kc)",
y = "Life Expectancy (years)",
title = "Bubbleplot"
)
p <- ggplot(data = gapminder2011,
aes(x = IncomePP,
y = LifeExpectancyYrs,
color = four_regions,
size = population,
shape = members_oecd_g77,
key = country)) +
geom_point(alpha = 0.4) +
scale_color_colorblind(
name = "Regions",
labels = c("Africa", "Americas",
"Asia", "Europe")
) +
scale_size(
name = "Population Size (millions)",
breaks = c(1e08,5e08,1e09),
labels = c(100,500,1000)
) +
hrbrthemes::theme_ipsum() +
labs(
x = "Income PP ($)",
y = "Life Expectancy (years)",
title = "Bubbleplot"
) +
scale_x_log10()
ggplotly(p)